$(function () {
    $("#jqGrid").Grid({
        url: '../auction/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '编号', name: 'id', index: 'id', width: 40},
            {label: '产品编号', name: 'pid', index: 'pid', width: 40},
			{label: '产品名称', name: 'pname', index: 'pname', width: 80},
			{label: '押金', name: 'deposit', index: 'deposit', width: 80},
			{label: '报名时间', name: 'enrollStart', index: 'enroll_start', width: 80,
             formatter:function(value){
			    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
            }
            },
			{label: '报名结束时间', name: 'enrollEnd', index: 'enroll_end', width: 80,
                formatter:function(value){
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }},
			{label: '开始时间', name: 'beginTime', index: 'begin_time', width: 80,
                formatter:function(value){
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }},
			{label: '结束时间', name: 'endTime', index: 'end_time', width: 80,
                formatter:function(value){
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }},
			{label: '起拍价', name: 'basePrice', index: 'base_price', width: 80},
			{label: '封顶价', name: 'cappedPrice', index: 'capped_price', width: 80},
			{label: '增长倍数', name: 'multiple', index: 'multiple', width: 80},
            {label: '状态', name: 'statu', index: 'statu', width: 80, formatter:function(value){
                    if(value==0){
                        return '关闭';
                    }else  if(value==1){
                        return '开启';
                    }
                }},
			{label: '创建时间', name: 'createDate', index: 'create_date', width: 80}
			]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		auction: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.auction = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.auction.id == null ? "../auction/save" : "../auction/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.auction),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../auction/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../auction/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.auction = r.auction;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        open: function (event) {
            let aid = getSelectedRows("#jqGrid");
            if (aid == null){
                return;
            }

            confirm('确定要开启竞拍结果？', function () {
                Ajax.request({
                    url: "../auction/open",
                    params: {"aid":aid},
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
	}
});